<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="物料ID" prop="materialId">
        <el-input
            v-model="queryParams.materialId"
            placeholder="请输入物料ID"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商批次ID" prop="supplierBatchId">
        <el-input
            v-model="queryParams.supplierBatchId"
            placeholder="请输入供应商批次ID"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="初始数量" prop="initialQuantity">
        <el-input
            v-model="queryParams.initialQuantity"
            placeholder="请输入初始数量"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="当前数量" prop="currentQuantity">
        <el-input
            v-model="queryParams.currentQuantity"
            placeholder="请输入当前数量，物料批次当前剩余数量"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生产日期" prop="productionDate">
        <el-date-picker clearable
                        v-model="queryParams.productionDate"
                        type="date"
                        value-format="YYYY-MM-DD"
                        placeholder="请选择生产日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="过期日期" prop="expirationDate">
        <el-date-picker clearable
                        v-model="queryParams.expirationDate"
                        type="date"
                        value-format="YYYY-MM-DD"
                        placeholder="请选择过期日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="入库时间" prop="inboundTime">
        <el-date-picker clearable
                        v-model="queryParams.inboundTime"
                        type="date"
                        value-format="YYYY-MM-DD"
                        placeholder="请选择入库时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="计划数量" prop="planQuantity">
        <el-input
            v-model="queryParams.planQuantity"
            placeholder="请输入计划数量"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['material-service:batches:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['material-service:batches:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['material-service:batches:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['material-service:batches:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="batchesList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="批次ID，唯一标识物料批次" align="center" prop="batchId" />-->
      <el-table-column label="物料ID" align="center" prop="materialId" />
<!--      <el-table-column label="供应商批次ID，可选的供应商侧批次标识" align="center" prop="supplierBatchId" />-->
      <el-table-column label="初始数量" align="center" prop="initialQuantity" />
      <el-table-column label="当前数量" align="center" prop="currentQuantity" />
      <el-table-column label="计划数量" align="center" prop="planQuantity" />
      <el-table-column label="计量单位，如个、千克等" align="center" prop="unitOfMeasure" />
      <el-table-column label="生产日期" align="center" prop="productionDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.productionDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="过期日期" align="center" prop="expirationDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.expirationDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="入库时间" align="center" prop="inboundTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.inboundTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="供应商ID" align="center" prop="supplierId" />
      <el-table-column label="存储位置ID" align="center" prop="storageLocationId" />
      <el-table-column label="是否合格，0表示不合格，1表示合格" align="center" prop="isQualified" />
      <el-table-column label="状态，默认InStock表示在库，可扩展其他状态" align="center" prop="status" />
      <el-table-column label="创建时间" align="center" prop="createdAt" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updatedAt" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.updatedAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['material-service:batches:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['material-service:batches:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改MES系统物料批次，用于管理物料批次全流程信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="batchesRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="物料ID" prop="materialId">
          <el-input v-model="form.materialId" placeholder="请输入物料ID，关联物料主表" />
        </el-form-item>
        <el-form-item label="供应商批次ID" prop="supplierBatchId" >
          <el-input v-model="form.supplierBatchId" placeholder="请输入供应商批次ID，可选的供应商侧批次标识" />
        </el-form-item>
        <el-form-item label="初始数量" prop="initialQuantity">
          <el-input v-model="form.initialQuantity" placeholder="请输入初始数量，物料批次的初始数量" />
        </el-form-item>
        <el-form-item label="当前数量" prop="currentQuantity">
          <el-input v-model="form.currentQuantity" placeholder="请输入当前数量，物料批次当前剩余数量" />
        </el-form-item>
        <el-form-item label="计量单位" prop="unitOfMeasure">
          <el-input v-model="form.unitOfMeasure" placeholder="请输入计量单位，如个、千克等" />
        </el-form-item>
        <el-form-item label="生产日期" prop="productionDate">
          <el-date-picker clearable
            v-model="form.productionDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择生产日期，物料的生产时间"

          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="过期日期" prop="expirationDate">
          <el-date-picker clearable
            v-model="form.expirationDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择过期日期，物料的过期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="入库时间" prop="inboundTime">
          <el-date-picker clearable
            v-model="form.inboundTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择入库时间，默认当前时间，记录物料入库时刻">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="供应商ID" prop="supplierId">
          <el-input v-model="form.supplierId" placeholder="请输入供应商ID，关联供应商主表" />
        </el-form-item>
        <el-form-item label="存储位置ID" prop="storageLocationId">
          <el-input v-model="form.storageLocationId" placeholder="请输入存储位置ID，关联存储位置主表" />
        </el-form-item>
        <el-form-item label="创建时间" prop="createdAt">
          <el-date-picker clearable
            v-model="form.createdAt"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择创建时间，记录记录创建时刻">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="更新时间" prop="updatedAt">
          <el-date-picker clearable
            v-model="form.updatedAt"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择更新时间，记录最后一次更新时刻，数据更新时自动变更">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Batches">
import { listBatches, getBatches, delBatches, addBatches, updateBatches } from "@/api/material-service/batches"

const { proxy } = getCurrentInstance()

const batchesList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    materialId: null,
    supplierBatchId: null,
    initialQuantity: null,
    currentQuantity: null,
    unitOfMeasure: null,
    productionDate: null,
    expirationDate: null,
    inboundTime: null,
    supplierId: null,
    storageLocationId: null,
    isQualified: null,
    status: null,
    createdAt: null,
    updatedAt: null
  },
  rules: {
    materialId: [
      { required: true, message: "物料ID，关联物料主表不能为空", trigger: "blur" }
    ],
    initialQuantity: [
      { required: true, message: "初始数量，物料批次的初始数量不能为空", trigger: "blur" }
    ],
    currentQuantity: [
      { required: true, message: "当前数量，物料批次当前剩余数量不能为空", trigger: "blur" }
    ],
    unitOfMeasure: [
      { required: true, message: "计量单位，如个、千克等不能为空", trigger: "blur" }
    ],
    storageLocationId: [
      { required: true, message: "存储位置ID，关联存储位置主表不能为空", trigger: "blur" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询MES系统物料批次，用于管理物料批次全流程信息列表 */
function getList() {
  loading.value = true
  listBatches(queryParams.value).then(response => {
    batchesList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    batchId: null,
    materialId: null,
    supplierBatchId: null,
    initialQuantity: null,
    currentQuantity: null,
    unitOfMeasure: null,
    productionDate: null,
    expirationDate: null,
    inboundTime: null,
    supplierId: null,
    storageLocationId: null,
    isQualified: null,
    status: null,
    createdAt: null,
    updatedAt: null
  }
  proxy.resetForm("batchesRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.batchId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加MES系统物料批次，用于管理物料批次全流程信息"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _batchId = row.batchId || ids.value
  getBatches(_batchId).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改MES系统物料批次，用于管理物料批次全流程信息"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["batchesRef"].validate(valid => {
    if (valid) {
      if (form.value.batchId != null) {
        updateBatches(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addBatches(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _batchIds = row.batchId || ids.value
  proxy.$modal.confirm('是否确认删除MES系统物料批次，用于管理物料批次全流程信息编号为"' + _batchIds + '"的数据项？').then(function() {
    return delBatches(_batchIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('material-service/batches/export', {
    ...queryParams.value
  }, `batches_${new Date().getTime()}.xlsx`)
}

getList()
</script>
